package com.hzh.cusviewdemo.Adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

public class BaseRecyclerViewHolder extends RecyclerView.ViewHolder {
    private final SparseArray<View> mViews;//集合类，layout里包含的View,以view的id作为key，value是view对象

    public BaseRecyclerViewHolder(Context context, View itemView) {
        super(itemView);
        Context context1 = context;
        mViews = new SparseArray<View>();
    }

    @SuppressWarnings("unchecked")
    public <T extends View> T findViewById(int viewId) {
        View view = mViews.get(viewId);
        if (view == null) {
            view = itemView.findViewById(viewId);
            mViews.put(viewId, view);
        }
        return (T) view;
    }

    private View getView(int viewId) {
        return findViewById(viewId);
    }

    public TextView getTextView(int viewId) {
        return (TextView) getView(viewId);
    }

    public Button getButton(int viewId) {
        return (Button) getView(viewId);
    }

    public ImageView getImageView(int viewId) {
        return (ImageView) getView(viewId);
    }

    public ImageButton getImageButton(int viewId) {
        return (ImageButton) getView(viewId);
    }

    public EditText getEditText(int viewId) {
        return (EditText) getView(viewId);
    }



    public BaseRecyclerViewHolder setText(int viewId, String value) {
        TextView view = findViewById(viewId);
        view.setText(value);
        return this;
    }

    public BaseRecyclerViewHolder setTextColor(int viewId, int color) {
        TextView view = findViewById(viewId);
        view.setTextColor(color);
        return this;
    }

    public BaseRecyclerViewHolder setOnClick(int viewId, View.OnClickListener listener) {
        View view = findViewById(viewId);
        view.setOnClickListener(listener);
        return this;
    }

    public BaseRecyclerViewHolder setOnLongClick(int viewId, View.OnLongClickListener listener) {
        View view = findViewById(viewId);
        view.setOnLongClickListener(listener);
        return this;
    }

    public BaseRecyclerViewHolder setBackground(int viewId, int resId) {
        View view = findViewById(viewId);
        view.setBackgroundResource(resId);
        return this;
    }

    public BaseRecyclerViewHolder setGone(int viewId, boolean gone) {
        getView(viewId).setVisibility(gone ? View.GONE : View.VISIBLE);
        return this;
    }

    public BaseRecyclerViewHolder setInVisible(int viewId, boolean gone) {
        getView(viewId).setVisibility(gone ? View.INVISIBLE : View.VISIBLE);
        return this;
    }

    public BaseRecyclerViewHolder setGlidePic(int viewId, boolean gone) {
        getView(viewId).setVisibility(gone ? View.INVISIBLE : View.VISIBLE);
        return this;
    }
}